﻿using System.Web.Mvc;
using Enterprise.Framework.Core.Container;
using Enterprise.Framework.Core.Exceptions;
using Enterprise.Framework.DomainModel;
using Resto.CostControl.Core;
using Resto.CostControl.Core.Facade.SystemModule.DataTransferObject;
using Resto.CostControl.Core.Facade.SystemModule.FacadeObject;
using Resto.CostControl.Core.Query.SystemModule.QueryObject;
using Resto.CostControl.Core.Query.SystemModule.ViewObject;
using Resto.CostControl.WebControl.DWZAdapter;
using Enterprise.Framework.DomainModel.Specifications;
using Resto.CostControl.WebControl.Service;

namespace Resto.CostControl.Web.Controllers
{
    public class DicController : Controller
    {

        public ActionResult Index(DWZPageInfo pageInfo)
        {
            DictionaryQO query = ObjectCreator.Create<DictionaryQO>();
            PagerCondition pager = pageInfo.ToPager();

            ISpecification<DictionaryVO> spec = null;
            if (!string.IsNullOrWhiteSpace(pageInfo.Keywords))
            {
                spec = Specification<DictionaryVO>.Create(p => p.Name == pageInfo.Keywords);
            }

            DictionaryVO[] array = query.QueryDictionaryVOByPager(spec, pager, "TypeCode", true);

            ViewBag.Total = pager.RecordCount;
            ViewBag.PageNum = pageInfo.PageIndex;
            ViewBag.NumPerPage = pageInfo.PageSize;
            ViewBag.OrderField = "TypeCode";
            ViewBag.OrderDirection = "asc";

            return View(array);
        }

        public ActionResult Edit(string Id)
        {
            DictionaryDTO data = null;
            if (Id != null)
            {
                DictionaryFO facade = ObjectCreator.Create<DictionaryFO>();

                data = facade.GetDictionaryDTO(Id);
            }
            else
            {
                data = new DictionaryDTO();
            }
            return View(data);
        }

        [HttpPost]
        public JsonResult Save(DictionaryDTO data)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    DictionaryFO facade = ObjectCreator.Create<DictionaryFO>();
                    if (!string.IsNullOrWhiteSpace(data.ID))
                        facade.UpdateDictionary(data);
                    else
                        facade.AddDictionary(data);

                }
                catch (BusinessValidationException ex)
                {
                    throw ex;
                }
            }

            var result = new FormReturnJson
            {
                statusCode = "200",
                message = "操作成功",
                navTabId = "Dic",
                callbackType = "closeCurrent",
                forwardUrl = "",
            };
            return Json(result);
        }



        [HttpPost]
        public JsonResult Delete(string id)
        {
            DictionaryFO facade = ObjectCreator.Create<DictionaryFO>();
            facade.DeleteDictionary(id);

            var result = new FormReturnJson
            {
                statusCode = "200",
                message = "操作成功",
                navTabId = "Dic",
                callbackType = "",
                forwardUrl = "",
            };
            return Json(result);
        }

        [HttpPost]
        public JsonResult Deletes(string[] Ids)
        {
            DictionaryFO facade = ObjectCreator.Create<DictionaryFO>();
            foreach (var id in Ids)
            {
                facade.DeleteDictionary(id);
            }
            var result = new FormReturnJson
            {
                statusCode = "200",
                message = "操作成功",
                navTabId = "Dic",
                callbackType = "",
                forwardUrl = "",
            };
            return Json(result);
        }
    }
}
